/**
* @Title: problemCheckList_Ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('ProblemCheckListCtrl', ['$scope','$state','$http','$modal','CaseAuditListService','CaseCommonService',
	                                           'BaseURL','Modal','FwaTabsetService','AuditDictServices','InsuranceService',
	                                function($scope,$state,$http,$modal,caseAuditListService,CaseCommonService
	                                		,baseUrl,Modal,fwaTabsetService,AuditDictServices,InsuranceService){
		
		$scope.auditDictServices = AuditDictServices;
		AuditDictServices.initCodeListDatas(); 
		
		InsuranceService.initMedicalOrgInfoList().success(function(response){
            $scope.medicalOrgInfoList = response.medicalOrgList;
		});
		
        var cellTemplate = '<a ng-show="data.problemStatus == 3" ng-click="operation(data);">审核</a>' +
            '<a ng-show="data.problemStatus == 6" ng-click="operation(data);">审核</a>';
		$scope.columns = [
  		{
			label : '事件号',
			map : 'problemNo',
			width : '10%',
			 cellTemplate:'<a  ng:bind="data.problemNo" '+
			 	'ng-click="queryByProblemCode(data)"></a>'}, 
		{
			label : '事件时间段',
			map : 'periodDate',
			width : '15%'
		}, {
			label : '事件要点',
			map : 'keyPoint',
			width : '20%'
		}, {
			label : '来源方式',
			map : 'sourceTypeDesc',
			width : '10%'
		}, {
			label : '提交日期',
			map : 'submitDate',
			width : '15%'
		}, {
			label : '关联案件号',
			map : 'otherCaseCode',
			width : '10%'
		}, {
			label : '状态',
			map : 'statusName',
			width : '10%'
		}, {
			label : '操作',
			map : 'operation',
			width : '10%',
            cellTemplate:cellTemplate
		}];
		
		$scope.operation = function(data){
			if(data.problemStatus == 3){//事件待审核
				$state.go('problemCheck.auditFindDispatch',{problemNo:data.problemNo,urlFlag:''});
			}else { //查看
				 $scope.queryByProblemCode(data);
			}
		}
		
		 $scope.queryByProblemCode = function(data){
             $state.go("problemDetailView.detail",{problemCode:data.problemNo,problemStatus:data.problemStatus});
		  }
		
		$scope.onReset = function(){
            $scope.params.problemCode=null;
            $scope.params.orgName=null;
            $scope.params.hearDate={start:null,end:null};
            $scope.params.sourceType=null;
            $scope.params.doctorName=null;
            $scope.params.jobLicense=null;
            $scope.params.sbmitDate={start:null,end:null};
	    };

       $scope.currentPage = 1;
        var start = 0;
        $scope.limit = 20;
        $scope.params = {
                page:0,
                pageSize:20
            };

        //分页监听
        $scope.$on('pageTool.skipPage',function(e,v){
            $scope.currentPage = v;
            start = $scope.limit*(v-1);
            $scope.onSubmit(true);
        });

	    $scope.onSubmit = function(resetPage){
	    	$scope.loading = true;
            if(resetPage == null || !resetPage){
	    		$scope.currentPage= 1;
	    		$scope.params.page = 0; 
	    	}else{
    			$scope.params.page = ($scope.currentPage - 1) * 20;
	    	}
            
	    	var fparam = $scope.params;
            if($scope.params.hearDate){
                var hearDateStart = $scope.params.hearDate.start;
                var hearDateEnd = $scope.params.hearDate.end;
                fparam.hearDateStart = hearDateStart;
                fparam.hearDateEnd = hearDateEnd;
            }
            if($scope.params.sbmitDate){
                var start = $scope.params.sbmitDate.start;
                var end = $scope.params.sbmitDate.end;
                fparam.start = start;
                fparam.end = end;
            }
	    	//查询条件少一个案件编号
	    	fparam.problemStatus ="\'3\'";
	    	CaseCommonService.loadProblemTask(fparam).success(function(response){
	    		$scope.loading = false;
	    		$scope.tableData=response.root;
	    		$scope.totalCount =response.total;
	    	});
	    	
	    }
	    
	    function getSourceType(code){
			if(code=='1'){
				return '其他部门转交';
			}else if(code=='2'){
				return '投诉';
			}else if(code=='3'){
				return '巡查';
			}else if(code=='4'){
				return '其他';
			}
		}
	    
	    $scope.onSubmit();
	    
	    
	    
	    
	    
	    /**
         * 获取勾选的总记录数
         */
        $scope.checkRows = [];
        
        function findCheck(d) {
         
        	for(var i=0;i< $scope.checkRows .length;i++) {
                 var checkedData =  $scope.checkRows [i];
               if(checkedData.problemNo == d.problemNo) {
                     return i;
                 }
             }
             return -1;
         }

         $scope.$on('fwatable.checked', function(e,d){
             if(findCheck(d) == -1) {
                 $scope.checkRows.push(d);
             }
         });

         $scope.$on('fwatable.unchecked', function(e,d){
             var index = findCheck(d);
             if(index != -1) {
                 $scope.checkRows.splice(index,1);
             }
         });
    
     /**
      * 批量提交稽核审核记录
      */
      $scope.batchSumit=function(){
    	  $scope.selectList=[{code:'1',label:'需要调查'},{code:'0',label:'无需调查'},{code:'2',label:'转其他部门处理'}];
    	  $scope.auditObj={};
    	  if($scope.checkRows.length>0){
    		  
    		    var flag=false;
    		    
    		 	for(var i=0;i< $scope.checkRows .length;i++) {
                    var checkedData =  $scope.checkRows [i];
                    var problemStatus=checkedData.problemStatus;
                    if(problemStatus!='3'){ //不是审核状态
                    	flag=true;
                    }
                }
    		if(flag){
    		 		 Modal.alert("只能选择审核状态进行批量操作!");
           		      return;
    		}else{
    			   popDialogFun();
    		     }
    			   
    		 
    	  }else{
    		  Modal.alert("请勾选要批量提交的记录!");
    		  return;
    	      }
      }
      
     /**
      * 批量操作弹出框
      */
     function popDialogFun(){
    	 var scope = $scope.$new();
 		var modalInstance = $modal.open({
 		      templateUrl: baseUrl+'views/hcpms/audit/caseCheck/caseCheckBatchSubmitDialog_view.html',
 		      backdrop:'static',
 			  size:'sm',
 			  scope:scope,
 			  controller: function($scope, $http,$modalInstance,Modal){
 				  
 				  
 				     //确定事件
     				 $scope.batchConfirm=function(){
     					 
     					 if($scope.auditObj.opinion){
     						 
    						  if($scope.auditObj.opinion=='0'){
	     				    	    if(!$scope.auditObj.opinionDesc){
	     				    	    	Modal.alert('意见描述不能为空!');
	     				    	    	return;
	     				    	    }
	     				       }
    						 
    					 }else{
    						Modal.alert('请选择审核意见!');
			    	    	return; 
    					 }
     				       
     				      var batchCaseCode=new Array();
     				 	
     				      for(var i=0;i< $scope.checkRows .length;i++) {
     				    	 var checkedData =  $scope.checkRows [i];
     				    	 var currentCaseCode=checkedData.problemNo;
     				    	 batchCaseCode.push(currentCaseCode);
     				 	    }
     				      var obj={
     				    		 caseCodes:batchCaseCode,
     				    		opinion:$scope.auditObj.opinion,
     				    		opinionDesc:$scope.auditObj.opinionDesc
     				    		
     				      };
     				     submitSelectAllRecord(obj,$scope.onClose);
     				 }
 				  
 				  
 					//关闭model
 					$scope.onClose = function() {
 						$modalInstance.close();
 					}
 					
 					
 			     }
 		      });
 			  
 		}
     
     
     
     /**
      * 开始提交批量选择的记录
      */
     function submitSelectAllRecord(parameter,closeFun){
    	$scope.loading = true;
    	$http({
				method : 'post',
				cache : false,
				url : baseUrl + 'auditFindDispatch/problemBatchSubmit.do',
				params :{
					caseCodes:parameter.caseCodes,
					opinion:parameter.opinion,
					opinionDesc:parameter.opinionDesc
				}
			}).success(function(response, status, headers, config) {
			     $scope.loading = false;
				  if(response.flag){
					  closeFun();
					  $scope.onSubmit();
					  Modal.alert(response.message);
				  }else{
					  closeFun();
					 Modal.alert(response.message);
				  }
			});
    	 
    	 
    	 
     }
	    
	    
	    
	    
	    
	}]);
})();
